package com.rjp.test;

import com.rjp.util.EmailTemplateHelper;
import com.rjp.util.SendMailHelper;
import com.rjp.bean.SwitchBean;
import org.apache.log4j.Logger;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class test {
    private static final Logger logger = Logger.getLogger(e.class);



    public static void main(String[] args) {
        List<SwitchBean> totalList = new ArrayList<>();
        List<SwitchBean> list = Collections.synchronizedList(testSwitchList());

        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        EmailTemplateHelper etl = new EmailTemplateHelper();
        etl.setXslEmailTemplate("C:\\Users\\ASUS\\Desktop\\aaa\\aaa\\src\\main\\resources\\update_switch_mac_email.xsl");
        etl.addElement("PROV_TIME", formatDate.format(new Date()));
        ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(30);
        try {
            int count = 0;
            for (SwitchBean switchBean : list) {
                count++;
                newFixedThreadPool.execute(() -> {
                    Map<String, String> rowMap = new HashMap<>();
                    if (switchBean.getSwitchModel().equals("CISCO 2960-24TC3") || switchBean.getSwitchMac().equals("001DE5532B802")) {
                        if (switchBean.getSwitchMac().equals("001DE5532B802")) {
                            rowMap.put("SNMPMAC", switchBean.getSwitchMac());
                            switchBean.setSwitchMac("***************");
                        }
                        if (switchBean.getSwitchModel().equals("CISCO 2960-24TC3")) {
                            rowMap.put("SNMPMODEL", switchBean.getSwitchModel());
                            switchBean.setSwitchModel("***************");
                        }
                        rowMap.put("SWITCHIP", switchBean.getSwitchIp());
                        rowMap.put("SWITCHMAC", switchBean.getSwitchMac());
                        rowMap.put("SWITCHMODEL", switchBean.getSwitchModel());
                        totalList.add(switchBean);
                    }
                    etl.addElement("TOTAL", String.valueOf(totalList.size()));
                    etl.addRowElement("LIST", rowMap);
                });
            }
            logger.debug("Thread run count is " + count);
            newFixedThreadPool.shutdown();
            testSendMail(etl,totalList);
        } catch (Exception e) {
            logger.error("unexpected: close push thread, may lost task ,cause user create excel or send email error ", e);
        }
    }

    public static List<SwitchBean> testSwitchList() {
        List<SwitchBean> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            SwitchBean switchBean = new SwitchBean();
            switchBean.setSwitchMac("001DE5532B80" + i);
            switchBean.setSwitchModel("CISCO 2960-24TC" + i);
            switchBean.setSwitchIp("10.240.19.71" + i);
            list.add(switchBean);
        }

        return list;
    }

    public static void testSendMail(EmailTemplateHelper etl,List<SwitchBean> totalList) {
        if (totalList.size() == 0) {
            etl.addElement("NODATA","There is no data to update");
        }

        SendMailHelper mailHelper = new SendMailHelper();
        mailHelper.setMailHost("pop3.cs.hkbn.com.hk");
        mailHelper.setMailFrom("marco.yuen@cs.hkbn.com.hk");
        mailHelper.setMailTo("blithe.tse@cs.hkbn.com.hk");
        mailHelper.setMailSubject("Update Switch MAC and Model Message Report");
        mailHelper.setContentHtml(true);
        /*SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        EmailTemplateHelper etl = new EmailTemplateHelper();

        etl.setXslEmailTemplate("C:\\Users\\marco.yuen\\Desktop\\marco_test_project\\spp_port_script\\src\\net\\hkbn\\prov\\swport\\script\\test\\update_switch_mac_email.xsl");
        etl.addElement("PROV_TIME", formatDate.format(new Date()));
        etl.addElement("TOTAL", String.valueOf(list.size()));

        for (SwitchBean switchBean : list) {
            Map<String,String> rowMap = new HashMap();
            rowMap.put("SWITCHIP", switchBean.getSwitchIp());
            rowMap.put("SWITCHMAC", switchBean.getSwitchMac());
            rowMap.put("SWITCHMODEL", switchBean.getSwitchModel());
            for (SwitchBean bean : beforeList) {
                rowMap.put("SNMPMAC", bean.getSwitchMac());
                rowMap.put("SNMPMODEL", bean.getSwitchModel());
            }
            etl.addRowElement("LIST", rowMap);
        }*/
        String result = etl.transEmailTemplate();
        System.out.println("Translate Result :\n" + result);
//        mailHelper.setMailContent(result);
//        mailHelper.send();
    }
}
